package org.yinxianren.www.study_02.study.java8;


import org.junit.Before;
import org.junit.Test;
import org.yinxianren.www.study_02.study.java8.entity.Apple;
import org.yinxianren.www.study_02.study.java8.interf.AppleFilter;

import java.io.File;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class Study01 {


    private List<Apple>  appleList=null;
    @Before
    public void be(){
        appleList=new ArrayList<>();
        appleList.add(new Apple("red",15));
        appleList.add(new Apple("green",8));
        appleList.add(new Apple("yellow",89));
        appleList.add(new Apple("black",7));
        appleList.add(new Apple("white",7));

        appleList.add(new Apple("red",19));
        appleList.add(new Apple("green",14));
        appleList.add(new Apple("yellow",20));
        appleList.add(new Apple("black",58));
        appleList.add(new Apple("white",17));

        appleList.add(new Apple("red",4));
        appleList.add(new Apple("green",17));
        appleList.add(new Apple("yellow",8));
        appleList.add(new Apple("black",93));
        appleList.add(new Apple("white",43));
    }

    @Test
    public void stduy_011(){
        Apple a=   appleList.stream()
                .distinct()
                .sorted((t1,t2)->t1.getWeight().compareTo(t2.getWeight()))
                .findFirst()
                .orElse(null);
        System.out.println(a.getWeight());
    }

    @Test
    public void stduy_01(){
        //1.测试方法的用处
//        File[]  fileArr= new File(".").listFiles();
//        List<File> fileList= Arrays.asList(fileArr);
//        fileList.forEach(System.out::println);
//                .\.git
//                .\.idea
//                .\pom.xml
//                .\src
//                .\target

        //2.案例一：用常规的方法
//        File[]  fileArr=new File(".").listFiles(new FileFilter() {
//            @Override
//            public boolean accept(File pathname) {
//                return pathname.isHidden();  //筛选出隐藏文件
//            }
//        });
//        List<File> fileList= Arrays.asList(fileArr);
//        fileList.forEach(System.out::println);
        //.\.git

        //3.案例一：使用java8 方法应用
        //用Java 8的方法引用::语法（即“把这个方法作为值”）将其传给listFiles方法
        //在Java 8里写下File::isHidden的时候，你就创建了一个方法引用，你同样可以传递它
        File[]  fileArr=new File(".").listFiles(File::isHidden);
        List<File> fileList= Arrays.asList(fileArr);
        fileList.forEach(System.out::println);
    }

    /**
     *  测试函数方法调用
     */
    @Test
    public void study_02(){
        List<Apple>  apples=getApple(appleList,(Apple apple)-> apple.getWeight()>50);
        apples.forEach(System.out::println);
    }

   private List<Apple> getApple(List<Apple> inventory, AppleFilter appleFilter){
       List<Apple>  apples=new ArrayList<>();
       for (Apple apple: inventory){
           if(appleFilter.test(apple)){
               apples.add(apple);
           }
       }
       return apples;
   }
   //排序测试
   @Test
   public void study_03(){
//       appleList.sort((apple_1,apple_2)->{
//           if(apple_1.getWeight()>apple_2.getWeight()) return 1;
//           if(apple_1.getWeight()<apple_2.getWeight()) return -1;
////           if(apple_1.getWeight()==apple_2.getWeight()) return 0;
//           return 0;
//       });
       //lambda
       appleList.sort((apple_1,apple_2)-> apple_1.getWeight().compareTo(apple_2.getWeight()));
       //函数调用
       appleList.sort(Comparator.comparing(Apple::getWeight));
       //排序逆向
       appleList.sort(Comparator.comparing(Apple::getWeight).reversed());
       //重量排序一样按颜色排序
       appleList.sort(Comparator.comparing(Apple::getWeight).thenComparing(Apple::getColor));

       appleList.forEach(System.out::println);

   }

   @Test
   public void study_04(){

       appleList.stream().filter(t->t.getWeight()>25)
                        .sorted(Comparator.comparing(Apple::getWeight))
                        .map(Apple::getColor)
//                        .reduce(t,Apple::getColor)
                        .collect(Collectors.toList())
                        .forEach(System.out::println);

   }

   @Test
   public void study_05(){
       String[] words={"hello ","world"};
       Arrays.stream(words).map(word->word.split("")).forEach(str->{
           String is=str[1];
       });
       System.out.println("-----------------------------------------");
       Arrays.stream(words).flatMap(word->Arrays.stream(word.split(""))).forEach(System.out::println);

   }

   @Test
   public void study_06(){
       Optional<String>  colorOptional= appleList.stream().map(apple -> apple.getColor()).filter(color->color.equals("grad")).findFirst();
       try {
           colorOptional.get();
       }catch (Exception e){
           System.out.println("未获取到相关的值");
       }
   }

    @Test
   public void study_07(){
       IntStream
               .range(0,50)
               .boxed()
               .map(a->a*a)
               .forEach(System.out::println);

   }

    @Test
   public  void study_08(){
        Optional<Integer>  totalWeight= appleList.stream()
                .filter(t->t.getColor().equals("red"))
                .map(t->t.getWeight())
                .reduce((t1,t2)->t1+t2);


        System.out.println(totalWeight.get());
   }

   @Test
   public void test_09(){
       Instant currentTime = Instant.now();  //当前的时间
       Instant subtractAfterTime = currentTime.plusSeconds(-60*60);     //6分钟之前的订单
       SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
       System.out.print(formatter.format(Date.from(subtractAfterTime)));
   }


}
